package com.xh.petdogexpert.expert.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xh.petdogexpert.base.BaseController;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.base.ResponseData;
import com.xh.petdogexpert.expert.dto.Disease;
import com.xh.petdogexpert.expert.dto.Dog;
import com.xh.petdogexpert.expert.dto.Symptom;
import com.xh.petdogexpert.expert.service.IDogService;
import com.xh.petdogexpert.expert.service.IExpertService;
import com.xh.petdogexpert.expert.service.ISymptomService;

/**
 * @author linxixin 专家系统选择问答的控制器
 */
@RequestMapping(value = "expert")
@Controller
public class ExpertController extends BaseController {

	@Autowired
	IDogService dogService;

	@Autowired
	ISymptomService symptomService;

	@Autowired
	IExpertService expertService;

	Logger logger = LoggerFactory.getLogger(ExpertController.class);

	/**
	 * 返回第一次显示的症状供用户选择,安卓接口
	 * 
	 * @author linxixin
	 * @Time 2016年3月22日
	 */
	@RequestMapping(value = "/getFirstSymptoms")
	@ResponseBody
	public ResponseData getFirstSymptoms(HttpServletRequest request, ModelAndView view) {

		List<Symptom> symptoms = symptomService.getLastTreeSymptoms(10);

		return new ResponseData(symptoms);
	}

	/**
	 * 显示专家系统功能使用的页面
	 * 
	 * @author linxixin
	 * @Time 2016年3月22日
	 */
	@RequestMapping(value = "/function")
	public ModelAndView showFunction(HttpServletRequest request, ModelAndView view) {

		List<Symptom> symptoms = symptomService.getLastTreeSymptoms(10);

		view.getModel().put("symptoms", symptoms);

		view.setViewName("expert/function");

		expertService.clearSession();
		return view;
	}

	/**
	 * 获取 多个症状id 转换成list<String>
	 * 
	 * @author linxixin
	 * @date 2016年3月23日
	 */
	@RequestMapping(value = "/getFirstCheckRet")
	@ResponseBody
	public ResponseData getFirstCheckRet(
			@RequestParam(name = "symptomsId[]", required = false) ArrayList<String> symptomsId,
			@RequestParam(name = "uncheckList[]", required = false) ArrayList<String> uncheckList) throws Exception {

		if (symptomsId == null) {
			throw new BaseException("请先选择症状");
		}

		if (uncheckList == null)
			uncheckList = new ArrayList<String>();

		List<Symptom> nSymptom = expertService.getFirstCheckRet(symptomsId, uncheckList);

		return new ResponseData(nSymptom);

	}

	@RequestMapping(value = "/getNextCheckRet")
	@ResponseBody
	public ResponseData getNextCheckRet(
			@RequestParam(name = "symptomsId[]", required = false) ArrayList<String> symptomsId,
			@RequestParam(name = "uncheckList[]", required = false) ArrayList<String> uncheckList) throws Exception {
		System.out.println(symptomsId);

		if (symptomsId == null) {
			throw new BaseException("请先选择症状");
		}

		if (uncheckList == null)
			uncheckList = new ArrayList<String>();

		List<Disease> diseases = expertService.getNextCheckRet(symptomsId, uncheckList);

		return new ResponseData(diseases);

	}

	@RequestMapping(value = "/choseAgain")
	@ResponseBody
	public ResponseData choseAgain() throws Exception {

		return new ResponseData(expertService.choseAgain());
	}

	@RequestMapping(value = "/finish")
	@ResponseBody
	public ResponseData finish() {
		boolean ok = expertService.finish();
		return new ResponseData(ok);
	}
	
	@RequestMapping(value = "/setDiseaseSymptom")
	public ModelAndView setDiseaseSymptom(Integer id) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/expert/setDiseaseSymptom");
		return modelAndView;
	}
}
